package com.tjetc.service.impl;

import com.tjetc.dao.ParkMapper;
import com.tjetc.domain.Park;
import com.tjetc.service.ParkService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

public class ParkServiceImpl implements ParkService {
    private ParkMapper parkMapper= Db.getMapper(ParkMapper.class);
    @Override
    public Result list() {
        return new Result(200,null,parkMapper.list());
    }

    @Override
    public Result getByDescription(String description) {
        Park park=parkMapper.getByDescription(description);
        if (park!=null){
            return new Result(200,null,park);
        }
        return new Result(500,null,null);
    }

    @Override
    public Result add(Park park) {
        int rows=parkMapper.insert(park);
        if (rows==1){
            return new Result(200,"添加成功",null);
        }else {
            return new Result(500,"添加失败",null);
        }
    }

    @Override
    public Result delete(int id) {
        Park park=parkMapper.getById(id);
        if (park==null){
            return new Result(500,"没有该园区",null);
        }
        int rows=parkMapper.deleteById(id);
        if (rows==1){
            return new Result(200,"删除成功",null);
        }
        return new Result(500,"删除失败",null);
    }

    @Override
    public Result update(Park park) {
        Park park1=parkMapper.getById(park.getParkId());
        if (park1==null){
            return new Result(500,"没有该园区",null);
        }
        Result result=getByDescription(park.getDescription());
        if (result.getCode()==200){
            return new Result(500,"该园区地址已经被占用",null);
        }
        int rows=parkMapper.update(park);
        if (rows==1){
            return new Result(200,"修改成功",null);
        }

        return new Result(500,"修改失败",null);
    }
}
